package com.jlect.swebing.renderers.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.jlect.swebing.ui.client.*;
import com.jlect.swebing.ui.client.button.GButton;
import com.jlect.swebing.ui.client.menu.GMenu;
import com.jlect.swebing.ui.client.menu.GMenuBar;
import com.jlect.swebing.ui.client.menu.GMenuItem;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTRendererSample implements EntryPoint {
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
//        firstLayout();
//        secondLayout();
//        thirdLayout();
//        lightPopupTest();
//        forthLayout();
        fontMetrixTest();
//        reflectionClassCreationTest();

//        Label label = new Label("label");
//        label.addMouseListener();
//        RootPanel.get("slot1").add(label);

        /*DOM.addEventPreview(new EventPreview() {
            public boolean onEventPreview(Event event) {
                System.out.print("Event appeared");
                return false;  //@todo implement me
            }
        });*/
//        Button btn = new Button("click me");
//        RootPanel.get().add(btn);
        /*button.addActionListener(new ClickListener() {
            public void onClick(Widget sender) {
                if (label.getText().equals(""))
                    label.setText("Hello World!");
                else
                    label.setText("");
            }
        });*/

        // Assume that the host HTML has elements defined whose
        // IDs are "slot1", "slot2".  In a real app, you probably would not want
        // to hard-code IDs.  Instead, you could, for example, search for all
        // elements with a particular CSS class and replace them with widgets.
        //
//        RootPanel.get("slot1").add(button);
//        RootPanel.get("slot2").add(label);
    }

    private void reflectionClassCreationTest() {
        GLabel label = (GLabel) GWT.create(GLabel.class);
        label.setText("aaa");
        GRootPanel panel = new GRootPanel();
        final GContainer container = panel.getContentPanel();
        container.setLayoutManager(new BorderLayout());
        container.setSize(new Dimension(500, 300));
        container.addComponent(label, BorderLayout.CENTER);
    }

    private void fontMetrixTest() {
        Label label = new Label("click me");
        RootPanel.get("slot1").add(label);
        System.out.println("label.getOffsetHeight() = " + label.getOffsetHeight());
        label.setVisible(false);
        System.out.println("label.getOffsetHeight() = " + label.getOffsetHeight());
        label.setVisible(true);
        Element elem = label.getElement();
        DOM.setStyleAttribute(elem, "background-color", "red");
        DOM.setStyleAttribute(elem, "position", "absolute");
        DOM.setStyleAttribute(elem, "left", 0 + "px");
        DOM.setStyleAttribute(elem, "top", 0 + "px");
        System.out.println("label.getOffsetHeight() = " + label.getOffsetHeight());

        DOM.setStyleAttribute(elem, "position", "absolute");
        DOM.setStyleAttribute(elem, "left", 0 + "px");
        DOM.setStyleAttribute(elem, "top", -Window.getClientHeight() + "px");
        System.out.println("label.getOffsetHeight() = " + label.getOffsetHeight());
    }

    private void lightPopupTest() {
        LightPopup popup = new LightPopup();
        FlowPanel flowPanel = new FlowPanel();
        flowPanel.add(new Label("kk-------------"));
        flowPanel.add(new Label("dd sfdsdf sdfsd"));
        flowPanel.add(new Label("ee+++++++++++++ "));
        popup.setWidget(flowPanel);
        popup.show();
    }

    private void forthLayout() {
        GRootPanel panel = new GRootPanel();
        final GContainer container = panel.getContentPanel();
        container.setLayoutManager(new BorderLayout());
        container.setSize(new Dimension(500, 300));

        GMenuBar menuBar = new GMenuBar();

        GMenu menu1 = new GMenu("Menu1");
        GMenuItem menuItem1 = new GMenuItem("menuItem1");
        GMenuItem menuItem2 = new GMenuItem("menuItem2");
        GMenuItem menuItem3 = new GMenuItem("menuItem3");
        menu1.addMenuItem(menuItem1);
        menu1.addMenuItem(menuItem2);
        menu1.addMenuItem(menuItem3);

        GMenu menu2 = new GMenu("Menu2");
        GMenuItem menuItem4 = new GMenuItem("menuItem1");
        GMenuItem menuItem5 = new GMenuItem("menuItem1");
        GMenuItem menuItem6 = new GMenuItem("menuItem1");
        menu2.addMenuItem(menuItem4);
        menu2.addMenuItem(menuItem5);
        menu2.addMenuItem(menuItem6);

        menuBar.addMenu(menu1);
        menuBar.addMenu(menu2);
        
        container.addComponent(menuBar, BorderLayout.NORTH);
    }

    private void thirdLayout() {
        GRootPanel panel = new GRootPanel();
        final GContainer container = panel.getContentPanel();
        container.setLayoutManager(new BorderLayout());
        container.setSize(new Dimension(500, 300));

        GButton center = new GButton("Center");
        center.setPreferredSize(new Dimension(100, 50));
        container.addComponent(center, BorderLayout.CENTER);

        GLabel north = new GLabel("North");
        north.setPreferredSize(new Dimension(100, 50));
        container.addComponent(north, BorderLayout.NORTH);

        GTextField east = new GTextField("East");
        east.setPreferredSize(new Dimension(100, 50));
        container.addComponent(east, BorderLayout.EAST);

        GButton west = new GButton("West");
        west.setPreferredSize(new Dimension(100, 50));
        container.addComponent(west, BorderLayout.WEST);

        final GButton south = new GButton("South");
        south.setPreferredSize(new Dimension(100, 50));
        container.addComponent(south, BorderLayout.SOUTH);

        south.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Dimension preferredSize = south.getPreferredSize();
                preferredSize.width +=10;
                preferredSize.height +=10;
                south.setPreferredSize(preferredSize);
                container.invalidate();
            }
        });

        center.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                container.setSize(new Dimension(container.getWidth() - 10, container.getHeight() + 10));
            }
        });
    }

    private void firstLayout() {
        GRootPanel panel = new GRootPanel();
        final GContainer container = panel.getContentPanel();
        container.setLayoutManager(new BorderLayout());
        container.setSize(new Dimension(500, 300));

        GButton center = new GButton("Center");
        center.setPreferredSize(new Dimension(100, 50));
        container.addComponent(center, BorderLayout.CENTER);

        GButton north = new GButton("North");
        north.setPreferredSize(new Dimension(100, 50));
        container.addComponent(north, BorderLayout.NORTH);

        GButton east = new GButton("East");
        east.setPreferredSize(new Dimension(100, 50));
        container.addComponent(east, BorderLayout.EAST);

        GButton west = new GButton("West");
        west.setPreferredSize(new Dimension(100, 50));
        container.addComponent(west, BorderLayout.WEST);

        final GButton south = new GButton("South");
        south.setPreferredSize(new Dimension(100, 50));
        container.addComponent(south, BorderLayout.SOUTH);

        south.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Dimension preferredSize = south.getPreferredSize();
                preferredSize.width +=10;
                preferredSize.height +=10;
                south.setPreferredSize(preferredSize);
                container.invalidate();
            }
        });

        center.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                container.setSize(new Dimension(container.getWidth() - 10, container.getHeight() + 10));
            }
        });
    }

    private void secondLayout() {
        GRootPanel slot2 = new GRootPanel();
        final GContainer container = slot2.getContentPanel();
        container.setLayoutManager(new FlowLayout());
        container.setSize(new Dimension(500, 300));

        final GButton first = new GButton("first");
        first.setPreferredSize(new Dimension(100, 50));
        container.addComponent(first);

        final GButton north = new GButton("North");
        north.setPreferredSize(new Dimension(100, 50));
        container.addComponent(north);

        GButton east = new GButton("East");
        east.setPreferredSize(new Dimension(100, 50));
        container.addComponent(east);

        GButton west = new GButton("West");
        west.setPreferredSize(new Dimension(100, 50));
        container.addComponent(west);

        GButton south = new GButton("South");
        south.setPreferredSize(new Dimension(100, 50));
        container.addComponent(south);

        first.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Dimension preferredSize = first.getPreferredSize();
                preferredSize.width +=10;
                preferredSize.height +=10;
                first.setPreferredSize(preferredSize);
                container.invalidate();
            }
        });

        north.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                container.setSize(new Dimension(container.getWidth() - 10, container.getHeight() + 10));
            }
        });
    }


}
